<template>
  <live-player
    autoplay
    mode="RTC"
    :src="url"
    sound-mode="speaker"
    :id="streamID"
    @statechange="playStatechange"
    @netstatus="playNetstatus"
    @error="playError"
  ></live-player>
</template>

<script>
import { CommonExpress } from "../../utils/wxmini/common"

export default {
  name: "uniZegoPlayer",
  props: {
    url: {
      type: String
    },
    streamID: {
      type: String
    }
  },
  mounted() {
    this.livePlayerCtx = uni.createLivePusherContext("livePusher", this)
  },
  methods: {
    async stopPlayingStream(streamID) {
      const stopRes = await CommonExpress.zegoEngine.stopPlayingStream(streamID)
      this.livePlayerCtx.stop()
      return stopRes
    },
    // live-player 拉流状态回调
    playStatechange(e) {
      if (e.detail.code === -1307) {
        uni.showToast({
          icon: "none",
          title: "网络断连，且经多次重连抢救无效，更多重试请自行重启推流"
        })
      }
      CommonExpress.zegoEngine.updatePlayerState(e.currentTarget.id, e)
    },

    // live-player 拉流网络状态通知
    playNetstatus(e) {
      CommonExpress.zegoEngine.updatePlayerNetStatus(e.currentTarget.id, e)
    },
    // live-player 错误
    playError(e) {
      console.warn("mytag player pusherError", e)
    }
  }
}
</script>

<style scoped>
live-pusher,
live-player {
  width: 45vw;
  height: 45vw;
  max-width: 200px;
  max-height: 200px;
  margin: 5rpx;
}
</style>
